package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Question;
import cn.wolfcode.luowowo.article.domain.QuestionContent;
import cn.wolfcode.luowowo.article.mapper.QuestionContentMapper;
import cn.wolfcode.luowowo.article.mapper.QuestionMapper;
import cn.wolfcode.luowowo.article.query.QuestionQuery;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.article.vo.ArticleQuestionStatisVO;
import cn.wolfcode.luowowo.article.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionContentMapper questionContentMapper;

    @Override
    public PageInfo query(QuestionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(questionMapper.selectForList(qo));
    }

    @Override
    public Long saveOrUpdate(Question question) {

        if (question.getId() == null) {
            question.setCreateTime(new Date());
            questionMapper.insert(question);
            QuestionContent questionContent = new QuestionContent();
            questionContent.setId(question.getId());
            questionContent.setContent(question.getContent().getContent());
            questionContentMapper.insert(questionContent);
        } else {
            questionMapper.updateByPrimaryKey(question);
        }
        return question.getId();
    }

    @Override
    public Question get(Long rid) {
        return questionMapper.selectByPrimaryKey(rid);
    }

    @Override
    public List<Question> list() {

        return questionMapper.selectAll();
    }

    @Override
    public void updateSatatisVO(ArticleQuestionStatisVO vo1) {
        questionMapper.updateStatisVO(vo1);
    }
}


